package com.tico.common.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tico.common.utils.RedisUtil;
import com.tico.common.utils.SpringContextUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

@Data
public class LoginUser {
    @Autowired
    private RedisUtil redisUtil;

    private HttpServletRequest request;
    private String adcode;
    private String cnName;
    private String deptId;
    private String locationId;
    private String loginName;
    private String userId;
    private String phonenumber;
    private String token;

    public LoginUser(HttpServletRequest request) {
        String token = request.getHeader("access_token");
        if (redisUtil == null) {
            redisUtil = SpringContextUtil.getBean(RedisUtil.class);
        }
        String o = (String) redisUtil.get("login:pro:" + token);
        JSONObject jsonObject = JSON.parseObject(o);
        this.token = token;
        this.adcode = adcode;
        this.cnName = jsonObject.getString("cnName");
        this.deptId = jsonObject.getString("deptId");
        this.locationId = jsonObject.getString("locationId");
        this.loginName = jsonObject.getString("loginName");
        this.userId = jsonObject.getString("userId");
        this.phonenumber = jsonObject.getString("phonenumber");
    }
}
